const app = getApp()
let qCloud = require("../../index.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    organName: "",
    realName: "",
    phone: "",
    kindType: "",
    applyCount: 0,
    status: '1',

    dept: false,
    approval: false,
    auth: false,
    supplier: false,
    procedure: false,
    warehouse: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo();
    this.getPermissions();

    let userInfo = qCloud.session.getUser();
    this.setData({
      kindType: userInfo.kindType,
      status: userInfo.status
    });

    this.getEnterpriseInfo();

    if (userInfo.status == '0') {
      this.getUserApplyCount();
    }
  },

  onShow() {
    this.getUserInfo();
    this.getPermissions();

    let userInfo = qCloud.session.getUser();
    this.setData({
      kindType: userInfo.kindType,
      status: userInfo.status
    });

    this.getEnterpriseInfo();

    if (userInfo.status == '0') {
      this.getUserApplyCount();
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserInfo();
    this.getPermissions();

    let userInfo = qCloud.session.getUser();
    this.setData({
      kindType: userInfo.kindType,
      status: userInfo.status
    });

    this.getEnterpriseInfo();

    if (userInfo.status == '0') {
      this.getUserApplyCount();
    }
  },

  getUserInfo: function () {
    let self = this;
    app.fetchApis(self, qCloud.api.getInfo, {}, "GET", function (e) {
      if (e.data.code == 200) {
        qCloud.session.setPermi(e.data.permissions);
        qCloud.session.setRole(e.data.role);
        qCloud.session.setUser(e.data.user);
      }
    });
  },

  getPermissions: function () {
    let permission = qCloud.session.getPermi();
    if (permission && permission.length > 0) {
      if (permission.indexOf("preauth:apcDept:list") != -1) {
        this.setData({
          dept: true
        });
      } else {
        this.setData({
          dept: false
        });
      }

      if (permission.indexOf("apc:supplier:list") != -1) {
        this.setData({
          supplier: true
        });
      } else {
        this.setData({
          supplier: false
        });
      }

      if (permission.indexOf("preauth:apcUser:list") != -1) {
        this.setData({
          auth: true
        });
      } else {
        this.setData({
          auth: false
        });
      }

      if (permission.indexOf("apc:procedure:list") != -1) {
        this.setData({
          procedure: true
        });
      } else {
        this.setData({
          procedure: false
        });
      }

      if (permission.indexOf("system:apcApproveUser:list") != -1) {
        this.setData({
          approval: true
        });
      } else {
        this.setData({
          approval: false
        });
      }

      if (permission.indexOf("apc:warehouse:list") != -1) {
        this.setData({
          warehouse: true
        });
      } else {
        this.setData({
          warehouse: false
        });
      }
    } else {
      this.setData({
        dept: false,
        approval: false,
        auth: false,
        supplier: false,
        procedure: false,
        warehouse: false,
      })
    }
  },

  // 获取用户审核数
  getUserApplyCount: function () {
    let self = this;
    let params = {};

    app.fetchApis(self, qCloud.api.getUserApplyCount, params, "GET", function (e) {
      let result = e.data;
      if (result.code == 200) {
        self.setData({
          applyCount: result.data
        });
      }
    });
  },

  getEnterpriseInfo: function () {
    let self = this;
    app.fetchApis(self, qCloud.api.getEnterpriseInfo, {}, "GET", function (e) {
      let result = e.data;
      if (result.code == 200) {
        self.setData({
          organName: result.data.enterpriseName,
          realName: result.data.legalName,
          phone: result.data.enterprisePhone
        })
      }
    });
  },

  departTab: function () {
    let self = this;
    let userInfo = qCloud.session.getUser();

    wx.showModal({
      title: '提示',
      content: '确定要主动离职吗？离职后无法查看该企业任何信息，请谨慎操作',
      complete: (res) => {
        if (res.confirm) {
          app.fetchApis(self, qCloud.api.departUser + userInfo.userId, {}, "POST", function (e) {
            if (e.data.code == 200) {
              userInfo.status = "2";
              self.setData({
                status: "2"
              })
              qCloud.session.setUser(userInfo);
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: "error"
              })
            }
          });
        }
      }
    })
  }

});
